package org.jiaplee.action;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Serializable;

import org.apache.struts2.ServletActionContext;
import org.jiaplee.beans.Image;
import org.jiaplee.dao.ImageDao;

import com.opensymphony.xwork2.ActionSupport;

public class ShowImageAction extends ActionSupport {

	private static final long serialVersionUID = -126350850167015341L;
	private ImageDao imageDao;
	private InputStream imageStream;
	

	private int serializable;

	public int getSerializable() {
		return serializable;
	}

	public void setSerializable(int serializable) {
		this.serializable = serializable;
	}

	public InputStream getImageStream() {
		return this.imageStream;
	}

	public void setImageStream(InputStream imageStream) {
		this.imageStream = imageStream;
	}

	public void setImageDao(ImageDao imageDao) {
		this.imageDao = imageDao;
	}

	@Override
	public String execute() {
		Image image = imageDao.getImage(serializable);
		this.imageStream = ServletActionContext.getServletContext().getResourceAsStream("/upload/"+image.getUuid());
		try {
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			int count = 0;
			while((count=this.imageStream.read())!=-1){
				out.print(count);
			}
			out.flush();
			out.close();
			this.imageStream.close();
		} catch (IOException e) {
		 
			e.printStackTrace();
		}
		return null;
	}

}
